﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Jzy.Model.Project;
using Jzy.DAL.Project;

using System.Data;

using Frame.Lib.Core.Base;
using Frame.Lib.Common;
using Frame.Lib.Utilities;

namespace Jzy.BLL.Project
{
    public class TaskChangeBO:CommonOperate<TaskChangeInfo>
    {
        private TaskChangeDAL _taskChang;
        protected override BaseDAL DAL
        {
            get
            {
                return _taskChang;
            }
        }

        public TaskChangeBO():base("PrjTaskChange")
        {
            _taskChang = new TaskChangeDAL();
        }

        public void Save(PrjTaskInfo newInfo, PrjTaskInfo oldInfo,string reason)
        {
            TaskChangeInfo info = new TaskChangeInfo();
            info.InputTime = DateTime.Now;
            info.Reason = reason;
            info.UserID = AppSession.UserID;
            info.UserName = AppSession.UserName;
            info.Changes = "";
            info.TaskID = oldInfo.ID;

            bool isChange = false;//判断是否发生变更
            if (newInfo.Name != oldInfo.Name)
            {
                isChange = true;
                info.Changes += "任务名称由[" + oldInfo.Name + "]=>[" + newInfo.Name + "],";
            }
            if (newInfo.IsMilestone != oldInfo.IsMilestone)
            {
                isChange = true;
                if (newInfo.IsMilestone)
                {
                    info.Changes += "该节点变成里程碑,";
                }
                else
                {
                    info.Changes += "取消该节点的进程碑,";
                }
            }
            if (newInfo.StartTime != oldInfo.StartTime)
            {
                isChange = true;
                info.Changes += "开始日期由[" + oldInfo.StartTime.ToString("yyyy-MM-dd") + "]=>[" + newInfo.StartTime.ToString("yyyy-MM-dd") + "],";
            }
            if (newInfo.EndTime != oldInfo.EndTime)
            {
                isChange = true;
                info.Changes += "结束日期由[" + oldInfo.EndTime.ToString("yyyy-MM-dd") + "]=>[" + newInfo.EndTime.ToString("yyyy-MM-dd") + "],";
            }
            if (newInfo.TimeLimit != oldInfo.TimeLimit)
            {
                isChange = true;
                info.Changes += "工期由[" + oldInfo.TimeLimit + "天]=>[" + newInfo.TimeLimit + "天],";
            }
            if (newInfo.Percent != oldInfo.Percent)
            {
                isChange = true;
                info.Changes += "完成进度由[" + oldInfo.Percent + "%]=>[" + newInfo.Percent + "%],";
            }
            if (newInfo.UserNames != oldInfo.UserNames)
            {
                isChange = true;
                List<string> newNames = StrUtil.SplitToStr(newInfo.UserNames);
                List<string> oldNames = StrUtil.SplitToStr(oldInfo.UserNames);
                List<string> addNames = new List<string>();
                List<string> delNames = new List<string>();
                foreach (string str in newNames)
                {
                    if (!oldNames.Contains(str))
                    {
                        addNames.Add(str);
                    }
                }
                foreach (string str in oldNames)
                {
                    if (!newNames.Contains(str))
                    {
                        delNames.Add(str);
                    }
                }
                if (addNames.Count > 0)
                {
                    info.Changes += "参与人员增加了[" + StrUtil.MergeToStr(addNames, ",") + "],";
                }
                if (delNames.Count > 0)
                {
                    if (addNames.Count > 0)
                    {
                        info.Changes += "删除了[" + StrUtil.MergeToStr(delNames, ",") + "],";
                    }
                    else
                    {
                        info.Changes += "参与人员删除了[" + StrUtil.MergeToStr(delNames, ",") + "],";
                    }
                }
            }
            if (newInfo.IsPrediction != oldInfo.IsPrediction)
            {
                isChange = true;
                if (newInfo.IsPrediction)
                {
                    info.Changes += "工期由[精确]=>[预估],";
                }
                else
                {
                    info.Changes += "工期由[预估]=>[精确],";
                }
            }

            info.Changes.TrimEnd(',');
            if (isChange)
            {
                base.Save(info);
            }
        }

        public int GetChangeCount(string taskID)
        {
            int count = _taskChang.GetChangeCount(taskID);
            return count;
        }

        public DataTable GetList(string TaskID)
        {
            DataTable dt = _taskChang.GetList(TaskID);
            return dt;

        }
    }
}
